package com.breaking.second.recycler;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.breaking.second.recycler.ViewHolder;

import com.breaking.second.R;
import com.breaking.second.databinding.ItemRecyclerBinding;
import com.breaking.second.databinding.LayoutRecyclerBinding;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import lombok.Setter;

public class XRecyclerView extends RecyclerView {
    @Setter
    private OnItemClickListener onItemClickListener;
    private final List<List<XRecyclerEntity>> entitys = new ArrayList<>();
    private Adapter adapter;
    private int recycler_margin, recycler_marginLeft, recycler_marginTop, recycler_marginRight, recycler_marginBottom, recycler_marginStart, recycler_marginEnd;

    public XRecyclerView(@NonNull Context context) {
        this(context, null);
    }

    public XRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
        initRecycler(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray a = getContext().getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.XRecyclerView,
                0, 0);
        try {
            // 从 TypedArray 中读取属性
            float recycler_margin = a.getDimension(R.styleable.XRecyclerView_recycler_margin, 0);
            float recycler_marginLeft = a.getDimension(R.styleable.XRecyclerView_recycler_marginLeft, 0);
            float recycler_marginTop = a.getDimension(R.styleable.XRecyclerView_recycler_marginTop, 0);
            float recycler_marginRight = a.getDimension(R.styleable.XRecyclerView_recycler_marginRight, 0);
            float recycler_marginBottom = a.getDimension(R.styleable.XRecyclerView_recycler_marginBottom, 0);
            float recycler_marginStart = a.getDimension(R.styleable.XRecyclerView_recycler_marginStart, 0);
            float recycler_marginEnd = a.getDimension(R.styleable.XRecyclerView_recycler_marginEnd, 0);
            this.recycler_margin = FloatToInt(recycler_margin);
            this.recycler_marginLeft = FloatToInt(recycler_marginLeft);
            this.recycler_marginTop = FloatToInt(recycler_marginTop);
            this.recycler_marginRight = FloatToInt(recycler_marginRight);
            this.recycler_marginStart = FloatToInt(recycler_marginStart);
            this.recycler_marginBottom = FloatToInt(recycler_marginBottom);
            this.recycler_marginEnd = FloatToInt(recycler_marginEnd);
        } finally {
            a.recycle(); // 确保 TypedArray 被回收
        }
    }

    private int FloatToInt(float f) {
        return (int) Math.ceil(f);
    }

    private void initRecycler(AttributeSet attrs) {
        adapter = new Adapter(entitys, attrs);
        setLayoutManager(new LinearLayoutManager(getContext()));
        setAdapter(adapter);
    }

    public void addEntity(List<XRecyclerEntity> entities) {
        this.entitys.add(entities);
        adapter.notifyItemChanged(this.entitys.size());
    }


    public void set(int i, List<XRecyclerEntity> entities) {
        this.entitys.set(i, entities);
        adapter.notifyItemChanged(this.entitys.size());
    }

    public class Adapter extends RecyclerView.Adapter<com.breaking.second.recycler.ViewHolder> {
        private final List<List<XRecyclerEntity>> entitys;
        private final AttributeSet attrs;

        public Adapter(List<List<XRecyclerEntity>> entitys, AttributeSet attrs1) {
            this.entitys = entitys;
            this.attrs = attrs1;
        }

        @Override
        public int getItemCount() {
            return entitys.size();
        }

        @NonNull
        @Override
        public com.breaking.second.recycler.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new com.breaking.second.recycler.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recycler, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull com.breaking.second.recycler.ViewHolder layoutRecyclerBindingViewHolder, int i) {
            LayoutRecyclerBinding binding = LayoutRecyclerBinding.bind(layoutRecyclerBindingViewHolder.itemView);
            if (binding.card.getLayoutParams() instanceof MarginLayoutParams layoutParams) {
                if (recycler_margin != 0) {
                    layoutParams.setMargins(recycler_margin, recycler_margin, recycler_margin, recycler_margin);
                } else {
                    layoutParams.bottomMargin = recycler_marginBottom;
                    layoutParams.leftMargin = recycler_marginLeft;
                    layoutParams.rightMargin = recycler_marginRight;
                    layoutParams.topMargin = recycler_marginTop;
                    if (recycler_marginStart != 0) {
                        layoutParams.leftMargin = recycler_marginStart;
                    }
                    if (recycler_marginEnd != 0) {
                        layoutParams.rightMargin = recycler_marginEnd;
                    }
                }
            }
            List<XRecyclerEntity> xRecyclerEntities = entitys.get(i);
            RecyclerView recyclerView = binding.recycler;
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            Adapter1 adapter1 = new Adapter1(xRecyclerEntities);
            recyclerView.setAdapter(adapter1);
        }
    }

    public class Adapter1 extends RecyclerView.Adapter<com.breaking.second.recycler.ViewHolder> {
        private final List<XRecyclerEntity> xRecyclerEntities;

        public Adapter1(List<XRecyclerEntity> xRecyclerEntities) {
            this.xRecyclerEntities = xRecyclerEntities;
        }

        @NonNull
        @Override
        public com.breaking.second.recycler.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new com.breaking.second.recycler.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull com.breaking.second.recycler.ViewHolder itemRecyclerBindingViewHolder, int i) {
            XRecyclerEntity entity = xRecyclerEntities.get(i);
            ItemRecyclerBinding binding = ItemRecyclerBinding.bind(itemRecyclerBindingViewHolder.itemView);
            binding.name.setText(entity.name);
            binding.getRoot().setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(entity);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return xRecyclerEntities.size();
        }
    }

    @Data
    public static class XRecyclerEntity {
        private String name;
        private String key;
        private int newsCount;

        public XRecyclerEntity(String name, String key) {
            this.name = name;
            this.key = key;
        }
    }
}
